import { Component, OnInit, ViewChild } from '@angular/core'
import { NzModalRef, NzMessageService } from 'ng-zorro-antd'
import { _HttpClient } from '@delon/theme'
import { SFSchema, SFUISchema, FormProperty, PropertyGroup, SFComponent } from '@delon/form'

import { User, IUpdateUser } from '@shared/interfaces/user'
import { UserService } from '../user.service'

@Component({
  selector: 'app-user-common-edit-info',
  templateUrl: './edit.component.html',
})
export class UserEditComponent implements OnInit {
  record: any = {}
  data: User
  @ViewChild('sf') sf: SFComponent
  schema: SFSchema = {
    properties: {
      userName: { type: 'string', title: '用户名', readOnly: true },
      nickName: { type: 'string', title: '显示名' },
      userType: { type: 'string', title: '用户类型' },
      mail: {
        type: 'string',
        title: '邮箱',
        // format: "email",
        ui: {
          validator: this.mailValidator,
        }
      },
      phoneNum: { type: 'string', title: '手机' },
      areaCode: { type: 'string', title: '地区编码' },
      agentCode: { type: 'string', title: '机构代码', ui: { validator: this.agentCodeValidator.bind(this)} },
      intMerCode: { type: 'string', title: '集团代码' },
      intStoreCode: { type: 'string', title: '商户代码' },
      insCode: { type: 'string', title: '门店代码' },
    },
    required: ['userName', 'nickName', 'mail', 'userType', 'agentCode'],
  }
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 100,
      grid: { span: 12 },
    },
    $no: {
      widget: 'text'
    },
    $href: {
      widget: 'string',
    },
    $description: {
      widget: 'textarea',
      grid: { span: 24 },
    }
  }

  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
    private userService: UserService,
  ) {}

  ngOnInit(): void {
  }

  save(value: any, valid: boolean): void {
    // let a: IUpdateUser
    // if (!valid) {
    //   console.log('参数非法')
    //   return
    // } else {
    //   const result = this.sf.validator()
    // }
    // this.userService.update(value).subscribe(res => {
    //   if (res instanceof Error) {
    //     this.msgSrv.error(res.message)
    //   } else {
    //     this.msgSrv.success(this.trans('GENERAL.NOTIFICATION.UPDATE_SUCCESS'))
    //     this.modal.close(true)
    //   }
    // })
  }

  close() {
    this.modal.destroy()
  }

  private mailValidator(value: any, formProperty: FormProperty, form: PropertyGroup) {
    /* 校验规则 */
    if (!value) {
      return [{ keyword: 'required', message: '必填项'}]
    } else if (value.length < 8) {
      return [{ keyword: 'minimum', message: '不能小于 8 位'}]
    } else {
      return []
    }
  }

  private agentCodeValidator(...args) {
    const { value } = this.validator.apply(this, args)
    if (!value) {
      return [{ keyword: 'required', message: '必填项'}]
    } else if (value.length < 8) {
      return [{ keyword: 'minimum', message: '不能小于 8 位'}]
    } else {
      return []
    }
  }

  private validator(value: any, formProperty: FormProperty, form: PropertyGroup) {
    return { value }
  }
}
